package com.colobu.rpcx.netty;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author goodjava@qq.com
 */
public class NettyEventExecuter implements Runnable {


    private static final Logger logger = LoggerFactory.getLogger(NettyEventExecuter.class);

    private final LinkedBlockingQueue<NettyEvent> eventQueue = new LinkedBlockingQueue<NettyEvent>();

    private final int maxSize = 10000;

    private volatile boolean stop = false;

    public void putNettyEvent(final NettyEvent event) {
        if (this.eventQueue.size() <= maxSize) {
            this.eventQueue.add(event);
        } else {
            logger.warn("event queue size[{}] enough, so drop this event {}", this.eventQueue.size(), event.toString());
        }
    }


    @Override
    public void run() {
        final ChannelEventListener listener = null;
        while (!this.isStoped()) {
            try {
                NettyEvent event = this.eventQueue.poll(3000, TimeUnit.MILLISECONDS);
                if (event != null && listener != null) {
                    switch (event.getType()) {
                        case IDLE:
                            listener.onChannelIdle(event.getRemoteAddr(), event.getChannel());
                            break;
                        case CLOSE:
                            listener.onChannelClose(event.getRemoteAddr(), event.getChannel());
                            break;
                        case CONNECT:
                            listener.onChannelConnect(event.getRemoteAddr(), event.getChannel());
                            break;
                        case EXCEPTION:
                            listener.onChannelException(event.getRemoteAddr(), event.getChannel());
                            break;
                        default:
                            break;

                    }
                }
            } catch (Exception e) {
                logger.warn(this.getServiceName() + " service has exception. ", e);
            }
        }
        logger.info(this.getServiceName() + " service end");
    }

    private boolean isStoped() {
        return stop;
    }

    public void shutdown() {
        this.stop = true;
    }

    private String getServiceName() {
        return null;
    }
}
